package DemoText;

/**
 * 外观数列
 */
public class LeetCode38 {
    public String countAndSay(int n) {
        String str = "1";
        if (n == 1) return str;
        for (int i = 2; i <= n; i++) {
            StringBuilder tmp = new StringBuilder();
            int left = 0, right = 0;
            int strn = str.length();
            while (right < strn) {
                while (right < strn && str.charAt(left) == str.charAt(right))
                    right++;
                int len = right - left;
                tmp.append(Integer.toString(len));
                tmp.append(str.charAt(left));
                left = right;
            }
            str = tmp.toString();
        }
        return str;
    }
}
